package be.rhok.eenzaamheidswijzer.controller;

import be.rhok.eenzaamheidswijzer.dto.QuestionDto;
import be.rhok.eenzaamheidswijzer.dto.SolutionDto;
import be.rhok.eenzaamheidswijzer.dto.StartTestDto;
import be.rhok.eenzaamheidswijzer.facade.LonelyFacade;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/*
 * http://localhost:8081/eenheidswijzer/web/loneliness/startTest
 * http://localhost:8081/eenheidswijzer/web/loneliness/question/1?token=2&answer=5
 * http://localhost:8081/eenheidswijzer/web/loneliness/solution/1?token=2&answer=5
 */


@Controller
@RequestMapping("/loneliness")
public class LonelyController {

    @Resource
    private LonelyFacade lonelyFacade;

    @RequestMapping(value = "/startTest", method = RequestMethod.GET)
    public ResponseEntity<StartTestDto> startTest(HttpServletRequest request) {
        String ip = request.getHeader("ClientIP");
        if (ip == null) {
            ip = request.getRemoteAddr();
        }

        StartTestDto startTest = lonelyFacade.startTest(ip);
        return new ResponseEntity<StartTestDto>(startTest, HttpStatus.OK);
    }


    @RequestMapping(value = "/question/{questionId}", method = RequestMethod.GET)
    public ResponseEntity<QuestionDto> getQuestion(@PathVariable("questionId") Long questionId, @RequestParam("answer") long answerId, @RequestParam("token") long token) {
        QuestionDto question = lonelyFacade.getQuestion(questionId, token, answerId);
        return new ResponseEntity<QuestionDto>(question, HttpStatus.OK);
    }

    @RequestMapping(value = "/solution/{solutionId}", method = RequestMethod.GET)
    public ResponseEntity<SolutionDto> getSolution(@PathVariable("solutionId") Long SolutionId, @RequestParam("answer") long answerId, @RequestParam("token") long token) {
        SolutionDto solution = lonelyFacade.getSolution(SolutionId, token, answerId);
        return new ResponseEntity<SolutionDto>(solution, HttpStatus.OK);
    }
}